import os
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers import Qwen2_5_VLForConditionalGeneration

# 量化模型测试 mlx-community

# 模型路径
model_dir = "/home/%s/models/mlx-community/Qwen2.5-VL-7B-Instruct-8bit" % os.getlogin()

# 加载tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
# 加载预训练模型
# model = AutoModelForCausalLM.from_pretrained(model_dir,
#                                              device_map="auto",
#                                              trust_remote_code=True,
#                                              load_in_8bit=True,
#                                              torch_dtype=torch.float16)model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
#     model_dir,
#     device_map="auto",
#     trust_remote_code=True,
#     load_in_8bit=True,
#     torch_dtype=torch.float16)

image_file = "/home/%s/data/image/haidianzhen/PH1.7-1-500/500-0000006A.jpg" % os.getlogin()

history = None

input_text = '获取图片的全部信息,输出为json格式'

response, history = model.chat(tokenizer, input_text, history=history)

print(response)

'''
文本+base64编码
import base64
from PIL import Image
from io import BytesIO
from transformers import AutoProcessor, AutoModelForCausalLM

# 假设 processor 是你的模型对应的处理器类
processor = AutoProcessor.from_pretrained("model-name")

# 示例输入字典，包含文本和 Base64 编码的图像
input_dict = {
    "text": "这张图片展示了什么？",
    "image": "..."
}

# 解码 Base64 图像
image_data = base64.b64decode(input_dict["image"].split(",")[1])
image = Image.open(BytesIO(image_data))

# 预处理图像
image_inputs = processor(images=image, return_tensors="pt")

# 预处理文本
text_inputs = processor(input_dict["text"], return_tensors="pt")

# 应用聊天模板（假设 apply_chat_template 方法接受文本和图像输入）
# 注意：这里的 apply_chat_template 是假设的方法，具体实现可能不同
formatted_inputs = processor.apply_chat_template(text=text_inputs, images=image_inputs)

# 假设模型是用于生成文本的模型
model = AutoModelForCausalLM.from_pretrained("model-name")

# 生成文本
outputs = model(**formatted_inputs)
'''

'''
import base64
from PIL import Image
from io import BytesIO
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
from qwen_vl_utils import process_vision_info

# 加载模型和处理器
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
    "Qwen/Qwen2.5-VL-7B-Instruct-AWQ", torch_dtype="auto", device_map="auto"
)
processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct-AWQ")

# 准备包含Base64编码图片的请求消息
messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "image",
                "image": "...",
            },
            {"type": "text", "text": "Describe this image."},
        ],
    }
]

# 准备推理
text = processor.apply_chat_template(
    messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
    text=[text],
    images=image_inputs,
    videos=video_inputs,
    padding=True,
    return_tensors="pt",
)
inputs = inputs.to("cuda")

# 推理：生成输出
generated_ids = model.generate(**inputs, max_new_tokens=128)
generated_ids_trimmed = [
    out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
    generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text)
'''
